<template>
  <el-form-item v-if="hookMixin.getShowValue(item)"
                :prop="`${propPrefix || ''}${item.field}`"
                :rules="item.rules"
                :class="[item.className || '', `is-${item.formType}`]"
                :style="{width: stylePercent ? stylePercent : (item.stylePercent ? `${item.stylePercent}%` : 'auto')}"
                class="wk-form-item">
    <!--下面是标题-->
    <template v-slot:label>
      {{ item.name }}
      <el-tooltip v-if="item.tipType == 'tooltip'" effect="dark" placement="top">
        <template v-slot:content>
          <div v-html="hookMixin.getTips(item)"/>
        </template>
        <i class="wk wk-help wk-help-tips"/>
      </el-tooltip>
      <span v-else style="color:#999;">
        {{ hookMixin.getTips(item) }}
      </span>
    </template>
    <!--下面是正文-->
    <WkField :item="item" :index="index" :field-from="fieldFrom" :ignore-fields="ignoreFields"
             :disabled="item.disabled || disabled" @change="fieldChange">
      <template v-slot:default="{ data, index }">
        <slot :data="data" :index="index"/>
      </template>
    </WkField>
  </el-form-item>
</template>

<script setup>
// item
import WkField from './WkField'
import {useHookMixin} from "components/NewCom/WkForm/HookMixin";

const hookMixin = useHookMixin()

defineOptions({
  name: 'WkFormItem',
})

const props = defineProps({
  // 表单验证前缀
  propPrefix: {
    type: String,
    default: ''
  },
  item: Object,
  index: Number,
  fieldFrom: {
    type: Object,
    default: () => {
      return {}
    }
  },
  // 忽略的字段直接输出
  ignoreFields: {
    type: Array,
    default: () => {
      return []
    }
  },
  stylePercent: String,
  disabled: Boolean
})

const emits = defineEmits(['change'])

function fieldChange(item, index, value, valueList) {
  console.log("fieldChange", item, index, value, valueList)
  emits('change', item, index, value, valueList)
}
</script>


<style lang="scss">
.wk-form-item {
  .el-form-item__label {
    line-height: 1.5;
    padding-bottom: 8px;
    word-break: break-all;
    word-wrap: break-word;
    color: #333;
  }

  .el-input {
    padding-right: 20px;
  }

  .el-form-item__error {
    position: relative;
    top: auto;
    left: auto;
  }

  .el-form-item.is-desc_text {
    .el-form-item__label {
      display: none;
    }
  }
}
</style>


<style lang="scss" scoped>
.wk-form-item {
  margin-top: 10px;
  margin-bottom: 0;

  .el-form-item__label {
    margin-top: 0px;
    margin-bottom: 20px;
  }

  .wk-select {
    width: 100%;
  }
}
</style>
